// Copyright (C) 2023 Beijing Huaxia Chunsong Technology Co., Ltd. 
// <https://www.chatopera.com>, Licensed under the Chunsong Public 
// License, Version 1.0  (the "License"), https://docs.cskefu.com/licenses/v1.html
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Copyright (C) 2018-Jun. 2023 Chatopera Inc, <https://www.chatopera.com>
// Licensed under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0
doctype html
html(xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3")
    head
        meta(charset="utf-8")
        meta(http-equiv="X-UA-Compatible" content="IE=edge,chrome=1")
        meta(http-equiv="Cache-Control" content="no-siteapp")
        meta(name="viewport" content="width=device-width, maximum-scale=1.0, initial-scale=1.0,initial-scale=1.0,user-scalable=no")
        meta(name="apple-mobile-web-app-capable" content="yes")
        meta(name="sourcecode-author", content="Beijing Huaxia Chunsong Technology Co., Ltd.")
        meta(name="sourcecode-license", content="Chunsong Public License, version 1.0, https://docs.cskefu.com/licenses/v1.html")
        title= systemConfig.title ? systemConfig.title : '春松客服 | 开源客服系统_联络中心'
        if systemConfig && systemConfig.favlogo
            link(rel="shortcut icon" type="image/x-icon" href='/res/image.html?id=' + systemConfig.favlogo)
        else
            link(rel="shortcut icon" type="image/x-icon" href='/images/favicon.ico')
        link(rel="stylesheet" href="/css/flexboxgrid.min.css")
        link(rel="stylesheet" type="text/css" href="/css/darktooltip.css")
        link(rel="stylesheet" href="css/layui.css")
        link(rel="stylesheet" href="/js/ztree/zTreeStyle/zTreeStyle.css")
        link(rel="stylesheet" href="/js/select/css/select2.min.css")
        link(rel="stylesheet" href="/res/css.html")
        link(rel="stylesheet" type="text/css" href="/js/kindeditor/themes/default/default.css")
        script(src="/js/jquery-1.10.2.min.js")
        script(src="/js/jquery.form.js")
        script(src="/js/select/js/select2.min.js")
        script(src="/js/moment.min.js")
        script(src="/js/moment-timezone.js")
        script(src="/js/moment-timezone-with-data.js")
        script(src="/layui.js")
        //- #894 和原生Map行为不一致
        //- <script src="/js/utils.js"></script>
        script(src="/js/cskefu.js")
        script(src="/im/js/socket.io.js")
        script(src="/js/CSKeFu_IM.v1.js")
        script(src="/js/CSKeFu_Rest_Request.v1.js")
        script(src="/js/CSKeFu_Agent_State.v1.js")
        script(src="/js/CSKeFu_Admin.v1.js")
        script(src="/js/ace/ace.js" type="text/javascript" charset="utf-8")
        script(src="/js/ace/theme-chrome.js" type="text/javascript" charset="utf-8")
        script(src="/js/weixinAudio.js")
        if systemConfig.mapkey
            script(type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=" + systemConfig.mapkey)
        style.
            .ztree li span {
                display: inline-block !important;
                white-space: pre-wrap;
                width: 90px;
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
            }
        script(src="/js/ztree/jquery.ztree.all.min.js")
        script(type="text/javascript" src="/js/kindeditor/kindeditor.js")
        script(type="text/javascript" src="/js/kindeditor/lang/zh-CN.js")
        script(type="text/javascript" src="/js/jquery.darktooltip.js")
        script(language="javascript").
            var hostname = "#{hostname}", adminuser = "#{user.admin ? true : false}",
                schema = "#{schema || 'http'}", port = "#{webimport}", userid = "#{user.id}",
                session = "#{sessionid}";
            var layinx, layerhelper;
            var isImIndex = false;
            $(document).ready(function () {
                layui.use('layer', function () {
                    layerhelper = layer;
                    var msg = '#{msg}';
                    if (msg == "security")
                        layer.msg('您访问的资源需要安全验证，请确认您有系统管理员权限！', {icon: 2, time: 3000})
                    else if (msg == 't0')
                        layer.msg('当前坐席就绪或对话未结束，不能切换#{systemConfig.namealias}', {icon: 2, time: 3000})
                    else if (msg == 't1')
                        layer.msg('当前用户坐席就绪或对话未结束，不能切换为非坐席', {icon: 2, time: 3000})

                    if ('#{models.contains("entim")}') {
                        var imDialogHelper = {
                            open: function () {
                                layinx = layer.open({
                                    type: 2,
                                    title: false,
                                    closeBtn: 0, //不显示关闭按钮
                                    shade: [0],
                                    area: ["120px", "50px"],
                                    offset: "rb", //右下角弹出
                                    anim: 2,
                                    shade: 0,
                                    content: "/ent/im/point.html",
                                    end: function () {
                                        if (isImIndex == true) {
                                            layinx = layer.open({
                                                type: 2,
                                                title: false,
                                                closeBtn: 0, //不显示关闭按钮
                                                shade: [0],
                                                area: ["30px", "50px"],
                                                offset: "rb", //右下角弹出
                                                anim: 2,
                                                shade: 0,
                                                content: "/ent/im/expand.html",
                                                end: function () { //此处用于演示
                                                    imDialogHelper.open();
                                                    isImIndex = false;
                                                }
                                            })
                                            isImIndex = false;
                                        } else {
                                            layinx = layer.open({
                                                type: 2,
                                                title: false,
                                                closeBtn: 0, //不显示关闭按钮
                                                shade: [0],
                                                area: ["260px", "520px"],
                                                offset: "rb", //右下角弹出
                                                anim: 2,
                                                shade: 0,
                                                content: ["/ent/im/index.html", "no"], //iframe的url，no代表不显示滚动条
                                                end: function () { //此处用于演示
                                                    imDialogHelper.open();
                                                }
                                            });
                                        }
                                    }
                                });
                            }
                        }
                        imDialogHelper.open();
                    }
                });
                $(".ukefu-left-menu").darkTooltip({
                    gravity: "west"
                });
            });
            function closeentim() {
                if (layerhelper) {
                    layerhelper.close(layinx);
                }
            }
            if ('#{tongjiBaiduSiteKey}') {
                var _hmt = _hmt || [];
                (function () {
                    var hm = document.createElement("script");
                    hm.src = "https://hm.baidu.com/hm.js?#{tongjiBaiduSiteKey}";
                    var s = document.getElementsByTagName("script")[0];
                    s.parentNode.insertBefore(hm, s);
                })();
            }
    body(οnunlοad="CloseOpen(event)")
        .layui-layout.layui-layout-admin
            .layui-header.header.header-ukefu
                .layui-main
                    a.logo(href="/")
                        if systemConfig && systemConfig.consolelogo
                            img(src='/res/image.html?id=' + systemConfig.consolelogo)
                        else
                            img(src='images/logo.png')
                    ul.layui-nav
                        li.layui-nav-item.layui-this
                            a.iframe_btn(href="javascript:void(0)" onclick="return false;" data-title="首页" data-href="/apps/content.html" data-id="maincontent" data-type="tabChange")
                                i.kfont(style="position: relative;") 
                                |  首页
                        li.layui-nav-item(style="position: relative;")
                            #ukefu-last-msg.ukefu-last-msg(data-num="0")
                                small#msgnum.ukefu-msg-tip.bg-red 0
                            a#agentdesktop.iframe_btn(href="javascript:void(0)" onclick="return false;" data-title="坐席工作台" data-href="/agent/index.html" data-id="multiMediaDialogWin" data-type="tabAdd")
                                i.kfont.icon-iconfontagent(style="position: relative;")
                                |  坐席
                        li.layui-nav-item
                            a.iframe_btn(href="javascript:void(0)" onclick="return false;" data-title="系统管理" data-href="/admin/content.html" data-id="admin" data-type="tabAdd")
                                i.layui-icon(style="position: relative;") 
                                |  系统
                        if organList && currentOrgan
                            li.layui-nav-item
                                a(href="javascript:void(0)")
                                    i.csfont(style="position: relative;float:left;") 
                                    span(style="display: inline-block;max-width: 90px;overflow:hidden;white-space:nowrap;text-overflow:ellipsis;float: left")= currentOrgan.name
                                dl.layui-nav-child
                                    dd
                                        div(style=" background: white;")
                                            ul#homeOrganTree.ztree(style="width: 240px;max-height: 800px;overflow-y: auto;")
                        li.layui-nav-item
                            a(href="javascript:void(0)")
                                i.layui-icon(style="position: relative;float:left;") 
                                span(style="display: inline-block;max-width: 90px;overflow:hidden;white-space:nowrap;text-overflow:ellipsis;float: left")= user.uname
                            dl.layui-nav-child
                                dd
                                    a(href="/apps/profile.html" data-toggle="ajax" data-width="750" data-title="修改资料") 个人资料
                                dd
                                    a(href="javascript:void(0)" onclick="showSystemBuildInfo()") 关于产品
                                dd
                                    a(href="https://docs.cskefu.com/" target="_blank") 使用指南
                                dd
                                    a(href="https://github.com/cskefu/cskefu/issues" target="_blank") 反馈建议
                                dd
                                    a(href="javascript:void(0)" onclick="quitSystem()") 退出系统
            .layui-side.layui-bg-black
                .layui-side-scroll
                    ul.layui-nav.layui-nav-tree.site-ukefu-nav
                        li.layui-nav-item.layui-nav-itemed
                            dl.layui-nav-child
                                dd.ukefu-left-menu
                                    a.iframe_btn(href="javascript:void(0)" onclick="return false;" data-title="首页" data-href="/apps/content.html" data-id="maincontent" data-type="tabChange")
                                        i.kfont(style="top: 1px;") 
                                if user && user.agent == true
                                    if user && (user.roleAuthMap["A01"] || user.admin)
                                        dd.ukefu-left-menu(data-tooltip="坐席工作台")
                                            a#agentdeskleft.iframe_btn(href="javascript:void(0)" onclick="return false;" data-title="坐席对话" data-href="/agent/index.html" data-id="multiMediaDialogWin" data-type="tabAdd")
                                                i.layui-icon(style="top: 3px;") &#xE63A;
                                if models.contains("contacts")
                                    if user && (user.roleAuthMap["A02"] || user.admin)
                                        dd.ukefu-left-menu(data-tooltip="全部联系人")
                                            a.iframe_btn(href="javascript:void(0)" data-title="全部联系人" onclick="return false;" data-href="/apps/contacts/index.html" data-id="contactsMainContentWin" data-type="tabAdd")
                                                i.layui-icon(style="position: relative;") 
                                    if user && (user.roleAuthMap["A03"] || user.admin)
                                        dd.ukefu-left-menu(data-tooltip="全部客户")
                                            a.iframe_btn(href="javascript:void(0)" data-title="全部客户" onclick="return false;" data-href="/apps/customer/index.html" data-id="customersMainContentWin" data-type="tabAdd")
                                                i.kfont(style="position: relative;") 
                                if models.contains("workorders")
                                    if user && (user.roleAuthMap["A04"] || user.admin)
                                        dd.ukefu-left-menu(data-tooltip="工单管理")
                                            a.iframe_btn(href="javascript:void(0)" data-title="工单管理" data-href="<#if systemConfig?? && systemConfig.workorders>/apps/workordersthree/index.html<#else>/apps/workorders/index.html</#if>" data-id="maincontent" data-type="tabChange")
                                                i.kfont(style="position: relative;") 
                                if models.contains("cca")
                                    if user && (user.roleAuthMap["A13_A01"] || user.admin)
                                        dd.ukefu-left-menu(data-tooltip="会话监控")
                                            a#customerchatsaudit.iframe_btn(href="javascript:void(0)" onclick="return false;" data-title="会话监控" data-href="/apps/cca/index.html" data-id="customerChatAudit" data-type="tabAdd")
                                                i.csfont(style="position: relative;") 
                                if currentOrgan && user && (user.roleAuthMap["A06"] || user.admin)
                                    dd.ukefu-left-menu(data-tooltip="客服设置")
                                        a.iframe_btn(href="javascript:void(0)" data-title="客服设置" data-href="/setting/agent/index.html" data-id="maincontent" data-type="tabChange")
                                            i.layui-icon(style="position: relative;") 
                                if currentOrgan && user && (user.roleAuthMap["A08"] || user.admin)
                                    dd.ukefu-left-menu(data-tooltip="会话历史")
                                        a.iframe_btn(href="javascript:void(0)" data-title="会话历史" data-href="/service/history/index.html" data-id="maincontent" data-type="tabChange")
                                            i.kfont(style="position: relative;") 
                                if models.contains("messenger") && currentOrgan && user && (user.roleAuthMap["A08"] || user.admin)
                                    dd.ukefu-left-menu(data-tooltip="营销中心")
                                        a.iframe_btn(href="javascript:void(0)" data-title="营销中心" data-href="/apps/messenger/otn/index.html" data-id="maincontent" data-type="tabChange")
                                            i.csfont(style="position: relative;") &#xe6c8;

            .layui-body
                .layui-tab.product-tab(lay-filter="ukefutab" lay-allowclose="true")
                    div(style="padding-bottom: 5px")
                        .ukefu-switch(style="position:relative;margin-left: 10px")
                            if user && user.agent == true
                                include /public/agentstatus
                        //- TODU 呼叫中心代码引入
                        //- if models.contain("callcenter")
                        //-    div(style="float: right")
                        //-        include /admin/channel/callcenter/softphone
                    ul.layui-tab-title
                        li.layui-this.ukefu-home(lay-id="maincontent")
                            i.kfont(style="position: relative;") 
                            |  首页
                    .layui-tab-content.product-content.ukefu-tab
                        .layui-tab-item.layui-show(style="height:100%;")
                            if vt
                                iframe#maincontent(frameborder="0" src='/apps/tenant/index.html' name="maincontent" width="100%" height="100%")
                            else
                                iframe#maincontent(frameborder="0" src='/apps/content.html' name="maincontent" width="100%" height="100%")
        script.
            var setting = {
                data: {
                    simpleData: {
                        enable: true
                    }
                },
                callback: {
                    onClick: onClick
                }
            };
            function onClick(e, treeId, treeNode) {
                if (treeNode.topic) {
                    $.post('/setorgan', {organ: treeNode.id}).success(function () {
                        location.href = '/';
                    })
                } else if (treeNode.id == "0") {
                    // location.href = "/admin/organ/index.html";
                } else if (treeNode.area) {
                    // $('#areaadd').attr("href", "/admin/organ/add.html?area=" + treeNode.id);
                }
            }
            var zNodes = !{pugHelper.toJSON(organList)};
            $.each(zNodes, function (i, o) {
                o.pId = o.parent;
                o.open = true;
                o.topic = true;
                o.icon = "/images/dept.png"
            })

            $(document).ready(function () {
                var ztree = $.fn.zTree.init($("#homeOrganTree"), setting, zNodes);
                if ('#{organData}') {
                    var node = ztree.getNodeByParam('id', '#{organData.id}');//获取id为1的点
                    ztree.selectNode(node);//选择点
                }
            });
            // 展示系统构建信息
            function showSystemBuildInfo() {
                layer.confirm('<div style="text-align:center;"> <img src="/images/favicon.ico" width="50px" height="50px"><p>春松客服<span>&#174;</span> Release #{appVersionNumber}</p></div>Build: #{appBuildDate}.<a href="https://github.com/cskefu/cskefu/commit/#{appVersionAbbrev}" target="_blank">#{appVersionAbbrev}</a> <br> Copyright (2018-2023) <a href="https://www.chatopera.com" target="_blank">Beijing Huaxia Chunsong Technology Co., Ltd.</a>, 开源许可协议: <a href="https://docs.cskefu.com/licenses/v1.html" target="_blank">Chunsong Public License, version 1.0</a> <br> <a href="https://docs.cskefu.com" target="_blank">文档中心</a> | <a href="https://www.cskefu.com/join-us/" target="_blank">加入开源社区</a> | <a href="https://www.chatopera.com/price.html" target="_blank">购买企业服务</a>',
                    {icon: -1, title: '关于产品', btn: ["关闭"]}, function (index, layero) {
                        //do something
                        layer.close(index);
                    });
            }

            // 登出系统
            // https://gitlab.chatopera.com/chatopera/cosinee.w4l/issues/291
            function quitSystem() {
                // 当坐席从就绪状态，切换到非就绪状态前，请求接口查看当前坐席服务到客户数。
                checkAgentStatusData().then(handleAgentStatusData)
                    .then(function (data) {
                        if (data.length == 0) {
                            // 执行登出
                            window.location.href = "/logout.html";
                        } else {
                            // 弹出对话框提示
                            layer.alert("您有正在服务中的访客" + data.length + "人！结束全部会话，或转接后可登出。", {title: "提示", icon: 2});
                        }
                    }, function (error) {
                        // 执行登出
                        window.location.href = "/logout.html";
                    });
            }
            //  获取时间差
            var timeDifference = #{timeDifference};
            var skills = '#{skills}'.split(',');
            layui.use('element', function () {
                var $ = layui.jquery, element = layui.element(); //Tab的切换功能，切换事件监听等，需要依赖element模块
                //触发事件
                $(".layui-tab").on("click", function (e) {
                    if ($(e.target).is(".layui-tab-close")) {
                        var win = $(e.target).parent().attr("lay-id");
                        if (win == "outboundCallConsole" && uKeFuSoftPhone) {
                            uKeFuSoftPhone.cancleoutbound();
                        }
                    }
                })
                $('.iframe_btn').on('click', function () {
                    var type = $(this).data('type');
                    if (type == "tabAdd") {
                        active.tabAdd($(this).data('href'), $(this).data('title'), $(this).data('id'));
                    } else if (type == "tabChange") {
                        active.tabChange($(this).data('href'), $(this).data('title'), $(this).data('id'));
                    }
                });
            });
            console.info("春松客服：迭代客服系统助力企业持续成长 https://www.cskefu.com/")
        if webrtc
            script.
                // 呼叫中心连接信息
                top.CSKEFU_WEBRTC = {
                    "ip": "#{webrtc.callCenterWebrtcIP}",
                    "port": "#{webrtc.callCenterWebRtcPort}",
                    "extension": "#{webrtc.callCenterExtensionNum}",
                    "password": "#{webrtc.callCenterExtensionPassword}",
                    "error": "#{webrtc.callCenterError}"
                }
                // TODO 打印前端中WebRTC链接信息，调试结束后删除打印日志
                // https://gitlab.chatopera.com/chatopera/cosinee/issues/926
                console.log("top.CSKEFU_WEBRTC", top.CSKEFU_WEBRTC);
